package com.cxk.hr.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cxk.hr.pojo.Employee;
import com.cxk.hr.pojo.Salary;
import com.cxk.hr.security.RoleSign;
import com.cxk.hr.service.EmployeeService;
import com.cxk.hr.service.SalaryService;
import com.cxk.hr.service.impl.EmployeeServiceImpl;
import com.cxk.hr.service.impl.SalaryServiceImpl;
import com.cxk.hr.util.MTimeUtil;

@Controller
@RequestMapping("/salary")
public class SalaryController {
	@Autowired
	SalaryService salaryService = new SalaryServiceImpl();
	@Autowired
	EmployeeService employeeService = new EmployeeServiceImpl();

	/*
	 * 无权限时返回的页面
	 */
	@RequestMapping("/unauthorized")
	public String toUnauthorized() {
		return "unauthorized";
	}

	@RequestMapping("/welcome")
	public String toWelcome() {
		return "welcome";
	}

	@RequestMapping("/{id}/detial")
	public String selectEmployee(@PathVariable Integer id, Model model) {
		Salary salry = salaryService.selectById(id);
		model.addAttribute("salry", salry);
		return "admin/salary_detail";
	}

	/**
	 * 未完成
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}/toUpdate")
	@RequiresRoles(value = RoleSign.ADMIN)
	public String toUpdate(Model model, @PathVariable Integer id) {
		return "admin/salary_update";
	}

	/**
	 * 未完成
	 * 
	 * @param id
	 * @param employee
	 * @param date
	 * @param status
	 * @param session
	 * @return
	 */
	@RequestMapping("/{id}/update")
	@RequiresRoles(value = RoleSign.ADMIN)
	public String updateById(@PathVariable Integer id, Employee employee, String date, String status,
			HttpSession session) {
		employee.setId(id);
		employee.setBirthday(MTimeUtil.stringParse(date));
		// 得到操作人员的名字
		Employee employee2 = (Employee) session.getAttribute("loged");
		employeeService.updateEmployee(employee, status, employee2.getName());
		return "forward:/salary/listPage";
	}

	@RequestMapping("/listPage")
	@RequiresRoles(value = RoleSign.ADMIN)
	public String selectList(Model model, Integer pageNo, Integer pageSize) {
		List<Salary> salaries = salaryService.findAll();
		model.addAttribute("salaries", salaries);
		return "admin/salary_list";
	}

	@RequestMapping("/listPage2")
	public String selectList2(Model model, Integer pageNo, Integer pageSize) {
		List<Salary> salaries = salaryService.findAll();
		model.addAttribute("salaries", salaries);
		return "admin/salary_list2";
	}

	@RequestMapping("/logout ")
	public String logout(HttpSession session) {
		session.removeAttribute("loged");
		// 登出操作
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "login";
	}

	@RequestMapping("/oneself/{id}/detial")
	public String selectEmployee2(@PathVariable Integer id, Model model) {
		Salary salary = salaryService.selectById(id);
		model.addAttribute("salary", salary);
		return "admin/oneself_detail";
	}

	@RequestMapping("/oneself/{id}/toUpdate")
	public String toUpdate2(Model model, @PathVariable Integer id) {
		return "admin/oneself_update";
	}

	@RequestMapping("/search")
	public String search(Model model, String input, Integer pageNo) {
		List<Salary> page = salaryService.search(input, pageNo);
		model.addAttribute("page", page);
		return "admin/search_result";
	}

	@RequestMapping("/toAdd")
	@RequiresRoles(value = RoleSign.ADMIN)
	public String toAdd(Model model) {
		return "admin/salary_add";
	}
}
